<? 
		if(isset($_POST['btnsubmit']))
		{
			require("required/dbconnect.php");
			 
			$id=$_POST['hidid'];
			$total_allow_select=$_POST["total_allow_select"];
			$gift_title=$_POST['gift_title'];
			$description=$_POST['description'];
			$status=$_POST['status'];
			
			$get_gift=mysql_query("select * from os_gift where product_id=$id");
				
			if(mysql_num_rows($get_gift)==0)
			{
				mysql_query("insert into os_gift (product_id,gift_title,description,total_allow_select,status)values($id,'$gift_title','$description','$total_allow_select','$status')")or die(mysql_error());
			}else{
				$gift_id=mysql_result($get_gift,0,"id");
				mysql_query("update os_gift set gift_title='$gift_title',description='$description',total_allow_select='$total_all_select',status='$status' where id=$gift_id")or die(mysql_error());
			}
		
			header("Location:gift_management.php?id=$id&action=3");
	
		}
		else
		{

				require("required/dbconnect.php"); 
				require("required/function.php"); 
				require("required/header.php"); 
	
?>
				<script src="required/js/jquery-1.4.2.js"></script>
				<script src="required/js/common_javascript.js"></script>
<?
				checkAccess("allow_product_edit",1); 
				$id=$_GET['id'];
				$get_id=mysql_query("select * from os_products where id=$id");
			
				$a=mysql_fetch_array($get_id,MYSQL_ASSOC);
				
				$get_gift=mysql_query("select * from os_gift where product_id=$id")or die(mysql_error());
				
				if(mysql_num_rows($get_gift)==0)
				{
					$gift_title="";
					$description="";
					$total_allow_select="";
					$status="";
					$can_add_item=0;
				}
				else
				{
					$r=mysql_fetch_array($get_gift,MYSQL_ASSOC);
					$gift_id=$r['id'];
					$gift_title=$r['gift_title'];
					$description=$r['description'];
					$total_allow_select=$r['total_allow_select'];
					$status=$r['status'];
					$can_add_item=1;
				}
				
				if(!empty($_GET['action'])){
					$action=$_GET['action'];
					
					switch($action){
						case 1:
							$msg="<div class='dw_success'>Record Updated</div>";
							break;
						case 3:
							$msg="<div class='dw_success'>Date Updated</div>";
							break;
						default:
							$msg="unknown";
							break;
					}
					echo "<script>displayMsg('divMsg')</script>";	
				}else{
					$msg="";
				}

?>
<script language="javascript">
	function validatefrm(frm)
	{
		if(document.frm.stock_amount.value=="")
		{
			alert("Please select total stock");
			document.frm.stock_amount.focus();
			return false;
		}
		
		return true;
	
	}
</script>
<table width="100%"  cellpadding="0" cellspacing="5" height="300">
	<script language="JavaScript" type="text/javascript" src="wysiwyg.js">
	</script>
	<script language="JavaScript" type="text/javascript" src="required/js/select_cat.js"></script>
	
	<td width="84%" align="left" valign="top">
		<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" onSubmit="return validatefrm(this)">
			<table width="100%">
				<td class="title" colspan="2"><strong>Gift Management</strong></td>
					<tr>
							<td colspan="3"> <div id="divMsg"><?= $msg ?></div></td>
						</tr>
				<tr valign="top">
					<td width="11%" colspan="2">Gift Management for <strong><?= $a['product_name_eng'] ?></strong>
					<br>
					
					
					
					</td>
				</tr>
				<tr>
                	<Td>Gift Title</Td><td><input type="text" name="gift_title" size="30" maxlength="100" value="<?= $gift_title ?>" /></td>
                </tr>
                <tr>
					<Td>Amount that allow to select</Td><td>
                    	<select name="total_allow_select">
                        	<?
								for($i=1;$i<=30;$i++)
								{
									if($total_allow_select==$i)
									{
										$slt="selected";
									}
									else
									{
										$slt="";
									}
							?>
                            <option value="<?= $i ?>" <?= $slt ?>><?= $i ?></option>
                            <? } ?>
                        </select>
                    </td>
				</tr>
                <Tr>
                	<Td>Status</Td>
                    <Td>
                    	<select name="status">
                        	<?
								if($status==1)
								{
									$sslt1="selected";
									$sslt0="";
								}
								else
								{
									$sslt1="";
									$sslt0="selected";
								}
							?>
                        	<option value="1" <?= $sslt1 ?> >Publish</option>
                            <option value="0" <?= $sslt0 ?> >Hide</option>
                        </select>
                    </Td>
                </Tr>
                
                <tr valign="top">
                    <td>Gift Description </td>
                    <td valign="top">
                        <? include_once("fckeditor/fckeditor.php") ?>
                        <?php
                            $oFCKeditor = new FCKeditor('description') ;
                            $oFCKeditor->ToolbarSet = 'DeswinBasic';
                            $oFCKeditor->BasePath = './fckeditor/' ;
                            $oFCKeditor->Value = $description ;
                            $oFCKeditor->Create() ;
                        ?>
                        
                    </td>
                </tr>
				
				
				
				<input type="hidden" value="<?= $id ?>" name="hidid">
				<tr>
					<td colspan="2"><input type="submit" class="button" value="Update" name="btnsubmit"> <input type="button" onClick="window.open('product_viewfull.php?id=<?= $id ?>','_self')" class="button" value="Back"></td>
				</tr>
				<tr>
					<td colspan="2">
					<br><br>
                    <?
						if($can_add_item==1)
						{
					?>
					<hr>
						<div class="sub_title">Gift Items</div>
                        <input type="button" value="Add Item" onclick="window.open('gift_add_item.php?gift_id=<?= $gift_id ?>','_self')"  />
						<table width="100%">
							<tr class="table_header">
								<td width="3%">No</td><td width="47%">Gift</td><Td width="34%">Action</td>
							</tr>
							<?
								$get=mysql_query("select a.product_id,b.id, b.product_name_eng,b.cat_id,b.sub_cat_id from os_gift_items a inner join os_products b on a.product_id=b.id where gift_id=$gift_id order by b.product_name_eng asc ")or die(mysql_error());
							
								if(mysql_num_rows($get)==0){
							?>
							<Tr>
								<td colspan="4" align="center"> - no record - </td>
							</Tr>
							<? 
								}else{ 
									$no=1;
									while($b=mysql_fetch_array($get,MYSQL_ASSOC)){
										if($no%2==0)
										{
											$class1="table_item_even";
											$class2="table_item_odd";
										}
										else
										{
											$class1="table_item_odd";
											$class2="table_item_even";
										}
										
										
										
								
							?>
							 <tr height="20" valign="top" class="<?= $class1 ?>"  onmouseover="this.className='table_item_mouseover'" 
                            onmouseout="this.className='<?= $class1 ?>'">
								<td><?= $no ?></td>
								<td><?= $b['product_name'] ?>(<?= getDataById("os_category","name_eng","id",$b['cat_id']) ?> - <?= getDataById("os_sub_category","name_eng","id",$b['sub_cat_id']) ?>) </td>
								<td>
                                	 <a href="branch_edit.php?id=<?= $a['id'] ?>">
                                         <img src="images/icons/edit.gif" border="0">
                                     </a>
                                     <a href="branch_delete.php?id=<?= $a['id'] ?>">
                                            <img src="images/icons/delete.gif" border="0">
                                     </a>
                                
                                </td>
							</tr>
							
							<? $no++; }} ?>
						
						</table>
					<? } ?>
					</td>
				</tr>
				
			</table>
		</form>
	</td>
 
</table>
<? }require("required/footer.php"); ?>